// pages/home/detail/detail.js
const app = getApp();
const util = require('../../../utils/util')
var { _get_activity_detail, _store_info, _focus_store, _collect_activity, _bind_phonenumber, _apply_bargain_activity, _bargain, _apply_bargain_list } = require('../../../utils/xiaohuangyu')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    remainingDate: '',//倒计时
    activityId: '',
    activityContent: '',  //活动内容
    storeInfo: {},
    activityDetail: {},
    swiperList: [{
      id: 0,
      type: 'image',
      url: '../../../images/banner1.jpg'
    }, {
      id: 1,
      type: 'image',
      url: '../../../images/banner2.jpg'
    }, {
      id: 2,
      type: 'image',
      url: '../../../images/banner1.jpg'
    }],
    stepList: [{
      name: '报名参加'
    }, {
      name: '找人砍价'
    }, {
      name: '购买'
    }, {
      name: '到店核销'
    },],
    TabCur: 0,
    navList: ['活动规则', '活动介绍', '评论'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityId: options['id'],
      userInfo: app.globalData.userInfo
    })
    this.initData()
  },
  initData() {
    // 获取活动详情
    _get_activity_detail(this.data.activityId, res => {
      if (res.status) {
        console.log('_get_activity_detail', res.data)
        // 倒计时
        let remainingDate = util.getRemainingDate(res.data.start_time, res.data.end_time)
        // 活动介绍
        const activityContent = app.towxml(res.data.content, 'markdown');
        this.setData({
          activityDetail: res.data,
          activityContent: activityContent,
          remainingDate: remainingDate
        })
      }
    })
    // 活动的商铺信息
    _store_info(this.data.activityId, res => {
      console.log('apply_storeInfo', res)
      this.setData({
        storeInfo: res.data
      })
    })
    // 报名列表
    let query_param = { activity: this.data.activityId, pageSize: 5 }
    _apply_bargain_list(this.data.activityId, query_param, res => {
      this.setData({
        apply_member_list:res.data.dataList,
        apply_member_list_totalResult:res.data.totalResult
      })
    })
  },
  // 关注
  focusTap(e) {
    _focus_store(e.currentTarget.dataset.store_id, res => {
      if (res.status) {
        wx.showToast({
          title: res.data,
          icon: 'none'
        })

        this.setData({
          [`storeInfo.is_focus`]: !this.data.storeInfo.is_focus
        })
      }
    })
  },
  // 报名用户
  viewActivePeopleTap() {
    wx.navigateTo({
      url: './people/people?id=' + this.data.activityId,
    })

  },
  // 我要报名
  signupTap() {
    // 报名
    let bargain_apply_id = ''
    if (!this.data.activityDetail.apply_info.is_apply) {
      _apply_bargain_activity(this.data.activityId, res => {
        if (res.status) {
          // 报名成功后给自己砍一刀
          bargain_apply_id = res.data
          _bargain(this.data.activityId, { 'bargain_apply_id': res.data }, reqs => {
            if (reqs.status) {
              wx.showToast({
                title: '报名成功',
              })
            }
          })
        }
      })
    } else {
      bargain_apply_id = this.data.activityDetail.apply_info.bargain_apply_id
    }
    wx.navigateTo({
      url: './detail/detail?id=' + this.data.activityId + '&bargain_apply_id=' + bargain_apply_id
    })
  },
  getPhoneNumber(res) {
    if (res.detail.encryptedData && res.detail.iv) {
      const data = {
        'iv': res.detail.iv,
        'encryptedData': res.detail.encryptedData
      }
      _bind_phonenumber(data, rqres => {
        if (rqres.status) {
          wx.setStorageSync('token', rqres.data.token)
          app.globalData.userInfo = rqres.data.userInfo
          wx.navigateTo({
            url: './detail/detail?id=' + this.data.activityId
          })
          this.setData({
            userInfo: rqres.data.userInfo
          })
        }
      })
    }
  },
  // 
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      // scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  // 收藏
  collectTap() {
    _collect_activity(this.data.activityId, res => {
      if (res.status) {
        this.setData({
          [`activityDetail.is_collect`]: !this.data.activityDetail.is_collect
        })
        wx.showToast({
          title: res.data,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})